package com.jinxinol.core.util;

import java.lang.reflect.Method;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>用于产生JSON文本的工具类。</p>
 * 
 */
public abstract class JSONGenerator {
	
	private static final DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	public static String toJSON(Object obj) {
		return JSONGenerator.addObject(obj);
	}
	
	@SuppressWarnings("rawtypes")
	public static String addObject(Object obj) {
		if (obj==null) {
			obj = "";
		}
		
		// 处理数组类型
		if (ClassTypeUtils.isArray(obj)) {
			return JSONGenerator.addArray((Object[])obj);
		}
		// 处理Map类型的对象
		if (ClassTypeUtils.isMap(obj)) {
			return JSONGenerator.addMap((Map)obj);
		}
		// 处理List类型的对象
		else if (ClassTypeUtils.isList(obj)) {
			return JSONGenerator.addList((List)obj);
		}
		// 处理日期类型
		else if (ClassTypeUtils.isDate(obj)) {
			return JSONGenerator.addDate((Date)obj);
		}
		// 处理简单类型和基本类型的对象
		else if (ClassTypeUtils.isBase(obj) || ClassTypeUtils.isSimple(obj) || ClassTypeUtils.isEnum(obj)) {
			return JSONGenerator.addBase(obj);
		}
		// 处理复杂类型的对象
		else {
			return JSONGenerator.addComplex(obj);
		}
	}
	
	private static String addArray(Object[] objArray) {
		String json = "[";
		int i=0;
		for (Object obj : objArray) {
			if (i > 0) {
				json += ", ";
			}
			i ++;
			json += JSONGenerator.addObject(obj);
		}
		json += "]";
		return json;
	}
	
	@SuppressWarnings("rawtypes")
	private static String addMap(Map map) {
		String json = "{\r\n";
		int i = 0;
		for (Object key : map.keySet()) {
			Object value = map.get(key);
			json += "\"" + key.toString() + "\" : ";
			json += JSONGenerator.addObject(value);
			if (i < map.keySet().size() - 1) {
				json += ",";
			}
			i ++;
			json += "\r\n";
		}
		json += "}\r\n";
		return json;
	}
	
	@SuppressWarnings({"rawtypes" })
	private static String addList(List list) {
		String json = "[";
		int i=0;
		for (Object value : list) {
			if (i > 0) {
				json += ", ";
			}
			i ++;
			json += JSONGenerator.addObject(value);
		}
		json += "]";
		return json;
	}
	
	private static String addDate(Date date) {
		String value = JSONGenerator.df.format(date).toString();
		return "\"" + value + "\"";
	}
	
	private static String addBase(Object obj) {
		String json = "";
		try {
			Method method = obj.getClass().getMethod("toString", new Class[0]);
			String value = method.invoke(obj).toString();
			json = (obj instanceof String) ? "\"" + value + "\"" : value; 
		} catch (Exception e) {
			System.out.println("not a base type");
		}
		return json;
	}
	
	private static String addComplex(Object obj) {
		String json = "{\r\n";
		Method methods[] = obj.getClass().getMethods();
		int i = 0;
		for (Method method : methods) {
			String methodName = method.getName();
			if(methodName.startsWith("get") && !methodName.equals("getClass") && method.getParameterTypes().length==0)
			{
				if (i > 0) {
					json += ", ";
				}
				i ++;
				methodName = methodName.substring(3);
				String mn = methodName.substring(0, 1).toLowerCase() + methodName.substring(1);
				json += "\"" + mn + "\" : ";
				try {
					Object value = method.invoke(obj);
					json += JSONGenerator.addObject(value);
				} catch (Exception e) {
					System.out.println("can not invoke method : " + methodName);
				}
			}
		}
		json += "\r\n}";
		return json;
	}
	
}

